<?php

class Community_StoryController extends Zend_Controller_Action
{

    public function init()
    {
        // Khởi tạo các thẻ [Lĩnh vực tìm kiếm]
        $category=new Admin_Model_MCategory();
        $bookCategory=$category->getAllCategories(array("_id","name"));
        $this->view->category=$bookCategory;        

        // Cài đặt thông tin đăng nhập
        $mySession=new Zend_Session_Namespace();
        if(isset ($mySession->uid) && isset($mySession->username))
        {
            $this->view->username=$mySession->username;
            $this->view->uid=$mySession->uid;

            $userModel=new Personal_Model_MUser();
            $this->view->userinfo=$userModel->getInfoBasic($mySession->uid);
        }
        
    }

    public function indexAction()
    {
        $this->_helper->layout()->setLayout('layout_community');
        $total=0;

        $currentpage=(int)$this->getRequest()->getParam('currentpage',1);
        $pagesize=10;
        $storyModel=new Community_Model_MStory();

        $story=$storyModel->getStories($total, $currentpage, $pagesize);
        $this->view->total=$total;
        if($total>0)
        {
            $this->view->story=$story;
            $this->view->currentpage=$currentpage;
            $this->view->pagesize=$pagesize;
        }
        
    }

    public function detailAction()
    {
        $this->_helper->layout()->setLayout('layout_community');
        $id=$this->getRequest()->getParam('id');

        $storyModel=new Community_Model_MStory();
        $story=$storyModel->getStory($id);
        if($story!=NULL)
        {
            $this->view->story=$story;
            $this->view->sid=$id;
        }
        else
            $this->_redirect ('/community');
    }

    public function addcommentAction()
    {
        $this->_helper->layout()->setLayout('layout_empty');
        $request=$this->getRequest();
        $mySession=new Zend_Session_Namespace();
        if(isset ($mySession->uid) && isset($mySession->username))
        {
            $uid=$request->getParam('uid');
            $sid=$request->getParam('sid');
            $content=$request->getParam('content');

            if($uid!=$mySession->uid)
            {
               $this->getResponse()->clearAllHeaders();
               $this->getResponse()->clearBody();
               $this->_redirect($this->getRequest()->getHeader("Referer"));
               return;
            }

            $storyModel=new Community_Model_MStory();
            $this->view->comment=$storyModel->addComment($uid,$sid,$content);
        }
    }
}

